/**
 * 
 */
package design_pattern.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;

/**
 * @author Administrator
 * 
 */
public class ConcretePrototype implements Prototype {


  /**
   * 浅复制
   */
  @Override
  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 深复制
   * 
   * @return
   * @throws IOException
   * @throws OptionalDataException
   * @throws ClassNotFoundException
   */
  @Override
  public Object deepClone() throws IOException, OptionalDataException, ClassNotFoundException

  {

    // write to stream
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(this);
    // oo.flush();

    // read from stream
    ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
    ObjectInputStream oi = new ObjectInputStream(bi);
    return (oi.readObject());

  }

}
